// this program processes the file "userpass.txt".

// does hashing by creating a hash table by userid.

// hash fucntion used is: took the ascii value of first letter and  take mod30 of it.

// best case: when all the userid's start with different characters.

//worst case:when all the userid's start with same characters.

// average case :when some have same starting character.

//writing to a file has been in the commented mode to write the file on your own
// please decomment lines from 83-109.(IMPORTANT PLZ READ)

//the text file I created will be attached in the mail.

#include<stdio.h>

#include<string.h>
struct node				// tok a array of structure to store username and
{						//password in the hash table.
	char user[40];
	char pass[20];
	
}n[30];					//size of hash table is 30, hence maximum 
						//posssible enteries can be 30 as 
						//required by open addressing scheme.(alpha<1)
int h=0;
void search()			//to search for the password of the userid and to verify   
{						//whether usrid exits and other checkings.
	int q,j;
	int i=0,k=0,flag=0;
	char password[20];
	char userid[40];
	printf("\nplease enter the userid max length is 40\n ");//max possible legnth 
														 //can be 40.
	scanf("%s",&userid);
	q=userid[0];		//getting the ascii value of first letter of username.
	do
	{
		j=(q+i)%30;		//hashfunction to find an entry from the hash table.
		if(strcmp(n[j].user,userid)==0)//checking whether userid entered is valid.
		{
			flag=1; 		//  variable set to 1 when userid matches
			while(k<3)		//allowing maximum tries to be 3.
			{
				printf("enter the password\n");
				scanf("%s",password);
				if(strcmp(n[j].pass,password)==0)
				{
					printf("yor account has been opened\n");
					h=1;
					break;
					//k=5;   		//just an aribitary value to get out of the loop
				}
				else
				{
					printf("you entered the wrong password\n");
					k++;
					if(k==3) 		//too many tries access denied
					{
						printf("you cannot open ur account\n");
						h=1;
					}	
				}
				
				
			}
		}
		else
		{
			i++;
		}	
	}while(i!=30 && strcmp(n[j].user,"")!=0 && h!=1);/*loop continues till i!=30 and 
											 userid block inthe hash table
											 is not empty*/
	if(flag==0)								 //if userid does not match
	{
		printf("the userid yo enterd could not be found\n");
	}
	
	
}
void main()
{

	int x,i,m,l;
	FILE *fp,*fp1;
	char ch='y';
	char user[40];
	char pass[20];
	char userid[40];
	
	/*struct rec				// structure to write username and password in 
								//the text file. 
	{
		char user[40];
		char pass[20];
	};
	struct rec a;
	fp=fopen("userpass.txt","w");		//openig file in write mode.
	if(fp==NULL)
	{
		
		puts("cannot open file");
		exit(1);
	}
	// writing the file and prompting user to add another record or not.
	while(ch=='y'||ch=='Y')
	{
		
		printf("\nenter the username and password seperated by only space");
		scanf("%s%s",&a.user,&a.pass);
		fprintf(fp,"%s %s\n",a.user,a.pass);
		printf("do you want to ad another username password(y/n)");
		fflush(stdin);
		scanf("%c",&ch);
		
	}
	fclose(fp);*/
	
	fp1=fopen("userpass.txt","r");  //opening the file inthe read mode
	if(fp==NULL)
	{
		
		puts("cannot open file");
		exit(1);
	}
	while(fscanf(fp1,"%s %s",user,pass)!=EOF)//scanning the recods from the file
											// till we encounter end of file
	{
		printf("\n%s %s\n",user,pass);//printing the userid and password
		l=user[0];                  //getting ascii value of 1st characterof userid.
		printf("the ascii value of 1st letter is:%d\n",l);			//printing l.
		i=0;
		while(i!=30)				//loop tiil 30 for probing at max 30 times.
		{
			x=user[0];
			m=(x+i)%30;				//linear probing on the hash function.
			if(strcmp(n[m].user,"")==0)	//putting userid and password in the
										// hash table when block is empty.
			{
				//p[m]=&n[m];
				strcpy(n[m].user,user);
				strcpy(n[m].pass,pass);
				//printf("\n%s %s",n[m].user,n[m].pass);//printing the userid password
				printf("index value of hash table is%d\n",m);	//printing the index of the hsh table.
				break;	
			}
			else
			{
				i++;
			}
		
		}
	}
	fclose(fp1);//closing the file using file pointer.
	
	
	search();	//calling the search function to do the further process.
		
	
	
}